﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using agent02.Model;

namespace agent02.Model
{
    public class RegisterModel
    {
        AgentDBEntities db = new AgentDBEntities();
        public KHACH_HANG khachHang { get; set; }
        public TAI_KHOAN taiKhoan { get; set; }
        public string password { get; set; }
        public RegisterModel()
        {
            khachHang = new KHACH_HANG();
            taiKhoan = new TAI_KHOAN();

            var kh = from k in db.KHACH_HANG
                     select k.MaKhachHang;
            int makh = kh.Count() + 1;
            string maKhachHang = "KH";
            if (makh < 10)
            {
                maKhachHang = maKhachHang + "00" + makh.ToString();
            }
            if (makh < 100)
            {
                maKhachHang = maKhachHang + "0" + makh.ToString();
            }
            else
            {
                maKhachHang += makh.ToString();
            }
            khachHang.MaKhachHang = maKhachHang;

            khachHang.NgayDangKy = DateTime.Now.Date;

            var tk = from t in db.TAI_KHOAN
                     where t.MaTaiKhoan.StartsWith("TK1")
                     select t.MaTaiKhoan;
            int matk = tk.Count() + 1;
            string maTaiKhoan = "TK1";
            if (matk < 10)
            {
                maTaiKhoan = maTaiKhoan + "00" + matk.ToString();
            }
            if (matk < 100)
            {
                maTaiKhoan = maTaiKhoan + "0" + matk.ToString();
            }
            else
            {
                maTaiKhoan += matk.ToString();
            }
            taiKhoan.MaTaiKhoan = maTaiKhoan;

            khachHang.MaTaiKhoan = maTaiKhoan;

            taiKhoan.TrangThai = 0;
        }
    }
}